package com.xyzh05.webapp.action;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.Preparable;
import com.xyzh05.model.Department;
import com.xyzh05.model.LoginLog;
import com.xyzh05.model.Notepad;
import com.xyzh05.model.Role;
import com.xyzh05.service.DictItemManager;
import com.xyzh05.service.NotepadManager;
import com.xyzh05.util.DateUtil;
import com.xyzh05.util.PageUtil;

public class NotepadAction extends BaseAction implements Preparable {
	private static final long serialVersionUID = 1085039356914430936L;
	
	private NotepadManager notepadManager;
    private DictItemManager dictItemManager;
    private List<Long> userIds = new ArrayList<Long>();
    private List<Long> deptIds = new ArrayList<Long>();
    private List<Integer> statisticList = new ArrayList<Integer>();
    private List<Notepad> notepadList = new ArrayList<Notepad>();
    private List<Department> deptList = new ArrayList<Department>();
    private List<Role> roleList = new ArrayList<Role>();
    private Notepad notepad;
    private Long id;
    private Short period;
    private Short type;
    private String content;
    private Integer currentPage;
    private Integer pageSize;
    private String query;
    private Integer actionType;
	private Integer resultCode;
	private Integer themeType;
	private Integer timeNum;

    public void setNotepadManager(NotepadManager notepadManager) {
        this.notepadManager = notepadManager;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
    }

	public Integer getThemeType() {
		return themeType;
	}

	public void setThemeType(Integer themeType) {
		this.themeType = themeType;
	}

	public Integer getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}

	public List<Department> getDeptList() {
		return deptList;
	}

	public void setDeptList(List<Department> deptList) {
		this.deptList = deptList;
	}

	public List<Long> getDeptIds() {
		return deptIds;
	}

	public void setDeptIds(List<Long> deptIds) {
		this.deptIds = deptIds;
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public void setQ(String q) {
        this.query = q;
    }

    public String list() {
    	userIds.add(getCurrentUser().getId());
    	deptIds.add(getCurrentDept().getId());
    	if(actionType == 2){
    		statisticList = notepadManager.getStatisticCount(userIds, Notepad.SIGN);
    	}else if(actionType == 3){
    		statisticList = notepadManager.getStatisticCount(userIds, Notepad.NOTE);
    	}else{
    		getDeptUsers();
    		statisticList = notepadManager.getStatisticCount(userIds, null);
    	}
        return SUCCESS;
    }

    private void getDeptUsers(){
		deptList = departmentManager.getDepartmentById(getCurrentDept().getId());
    	for(Department dept : deptList){
    		deptIds.add(dept.getId());
    	}
		roleList = roleManager.getUserBindList(deptIds,null, Role.DEFAULT_ROLE_EMPLOYEE.shortValue(), null);
		if(roleList != null && roleList.size() > 0){
			userIds.clear();
			for(Role r : roleList){
				userIds.add(r.getUser().getId());
			}
		}
    } 
    
	public List<Integer> getStatisticList() {
		return statisticList;
	}

	public void setStatisticList(List<Integer> statisticList) {
		this.statisticList = statisticList;
	}

	public void setId(Long id) {
        this.id = id;
    }

    public Notepad getNotepad() {
        return notepad;
    }

    public void setNotepad(Notepad notepad) {
        this.notepad = notepad;
    }

    public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public Integer getActionType() {
		return actionType;
	}

	public void setActionType(Integer actionType) {
		this.actionType = actionType;
	}

	public NotepadManager getNotepadManager() {
		return notepadManager;
	}

	public Long getId() {
		return id;
	}

	public Short getPeriod() {
		return period;
	}

	public void setPeriod(Short period) {
		this.period = period;
	}

	public Short getType() {
		return type;
	}

	public void setType(Short type) {
		this.type = type;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public List<Notepad> getNotepadList() {
		return notepadList;
	}

	public void setNotepadList(List<Notepad> notepadList) {
		this.notepadList = notepadList;
	}

	public String delete() {
        notepadManager.remove(notepad.getId());
        saveMessage(getText("notepad.deleted"));

        return SUCCESS;
    }

    public Integer getResultCode() {
		return resultCode;
	}

	public void setResultCode(Integer resultCode) {
		this.resultCode = resultCode;
	}

	public DictItemManager getDictItemManager() {
		return dictItemManager;
	}

	public void setDictItemManager(DictItemManager dictItemManager) {
		this.dictItemManager = dictItemManager;
	}

	public List<Long> getUserIds() {
		return userIds;
	}

	public void setUserIds(List<Long> userIds) {
		this.userIds = userIds;
	}

	public Integer getTimeNum() {
		return timeNum;
	}

	public void setTimeNum(Integer timeNum) {
		this.timeNum = timeNum;
	}

	
	public String edit() {
        if (id != null) {
            notepad = notepadManager.get(id);
        } else {
            notepad = new Notepad();
        }

        return SUCCESS;
    }

    /**
     * 保存记事、签到信息
     * @return
     * @throws Exception
     */
    public String save() throws Exception {
    	notepad = new Notepad();
    	notepad.setUser(getCurrentUser());
    	notepad.setSignInTime(DateUtil.getNow());
		notepad.setMode(LoginLog.MODE_PC);
		notepad.setType(type);
		notepad.setContent(content);
		
    	if (actionType == 1) {
    		notepad.setPeriod(period);
			notepad.setDictItem(dictItemManager.get(Notepad.SIGN.longValue()));
		} else {			
			notepad.setDictItem(dictItemManager.get(Notepad.NOTE.longValue()));
		}
    	
		try {
			notepad = notepadManager.save(notepad);
            resultCode = 1;
		} catch (Exception e) {
            resultCode = 0;
		}
    	return SUCCESS;
    }
    
    public String setSign(){
    	timeNum = 6;
    	return SUCCESS;
    }
    
    public String setNote(){
    	return SUCCESS;
    }
    
    /**
     * 加载签到、记事信息
     * @return
     */
    public String searchNotepad(){
    	userIds.add(getCurrentUser().getId());
    	deptIds.add(getCurrentDept().getId());
    	if(getCurrentUser().isCompanyRole()){
    		getDeptUsers();
    	}
    	if(actionType == 1){
    		notepadList = notepadManager.searchNotepad(userIds, null, 0, 10);
    		return "new";
    	}else if(actionType == 2){
    		if(themeType != null){
    	    	statisticList = notepadManager.getStatisticCount(userIds, Notepad.SIGN);
    		}
    		notepadList = notepadManager.searchNotepad(userIds, Notepad.SIGN, currentPage==null&&pageSize==null?0:PageUtil.getStartNum(currentPage, pageSize), pageSize==null?10:pageSize);
    		return "signin";
    	}else if(actionType == 3){
    		if(themeType != null){
    	    	statisticList = notepadManager.getStatisticCount(userIds, Notepad.NOTE);
    		}
    		notepadList = notepadManager.searchNotepad(userIds, Notepad.NOTE, currentPage==null&&pageSize==null?0:PageUtil.getStartNum(currentPage, pageSize), pageSize==null?10:pageSize);
    		return "notepad";
    	}else if(actionType == 4){
    		getDeptUsers();
    		notepadList = notepadManager.searchNotepad(userIds, null, currentPage==null&&pageSize==null?0:PageUtil.getStartNum(currentPage, pageSize), pageSize==null?10:pageSize);
    		return "group";
    	}
    	return SUCCESS;
    }
    
    public String statistic(){
    	return SUCCESS;
    }
}